package com.example.vue_shop.components.user.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue_shop.components.role.entity.Role;
import com.example.vue_shop.components.role.mapper.RoleMapper;
import com.example.vue_shop.components.user.entity.User;
import com.example.vue_shop.components.user.entity.UserPage;
import com.example.vue_shop.components.user.mapper.UserMapper;
import com.example.vue_shop.components.user.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lipengwei
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource(name = "userMapper")
    private UserMapper userMapper;

    @Resource(name = "roleMapper")
    private RoleMapper roleMapper;

    @Override
    public UserPage getUserList(String username, int pageNum, int pageSize) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(username)) {
            queryWrapper.like("username", username);
        }

        Page<User> page = new Page<>(pageNum, pageSize);

        IPage<User> userPage1 = userMapper.selectPage(page, queryWrapper);
        List<User> records = userPage1.getRecords();
        List<Map<String, Object>> userList = new ArrayList<>();

        records.forEach(user -> {
            Role role = roleMapper.getRole(user.getId());
            Map<String, Object> userMap = new HashMap<>(5);
            userMap.put("roleName", role == null ? "" : role.getRoleName());
            userMap.put("id", user.getId());
            userMap.put("username", user.getUsername());
            userMap.put("password", user.getPassword());
            userMap.put("userStatus", user.getUserStatus());
            userList.add(userMap);
        });

        UserPage userPage = new UserPage();
        userPage.setPageNum(pageNum);
        userPage.setPageSize(pageSize);
        userPage.setUserList(userList);
        userPage.setTotal(Integer.parseInt(userPage1.getTotal() + ""));
        return userPage;
    }

    @Override
    public void updateUserState(User user) {
        User user1 = userMapper.selectById(user.getId());
        user1.setUserStatus(user.getUserStatus());
        userMapper.updateById(user);
    }

    @Override
    public void addUser(User user) {
        userMapper.insert(user);
    }

    @Override
    public User getUserById(Long id) {
        return userMapper.selectById(id);
    }

    @Override
    public void updatePassword(int id, String password) {
        User user = userMapper.selectById(id);
        user.setPassword(password);
        userMapper.updateById(user);
    }

    @Override
    public void del(int id) {
        userMapper.deleteById(id);
    }
}
